using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;

using System.Data.Common;
using System.Data;

namespace Adoor.VSDesigner
{
    internal class IObjectResolverEditor : ObjectSelectorEditor
    {
        public IObjectResolverEditor()
            : base(true)
        {
        }



        public override object EditValue(ITypeDescriptorContext context, IServiceProvider serviceProvider, object value)
        {
            if (serviceProvider != null)
            {
                object obj1 = base.EditValue(context, serviceProvider, value);
                if (obj1 is Type)
                {
                    Type type1 = (Type)obj1;
                    IDesignerHost host1 = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost));

                    if (host1 != null && type1 is IComponent)
                    {
                        value = host1.CreateComponent(type1);
                    }
                    else
                    {
                        value = Activator.CreateInstance(type1);
                    }

                    //if (value != null)
                    //    ((Adoor.Relational.DbconnectionStringProvider)value)= "";


                    return value;
                }
                value = obj1;
            }
            return value;
        }

        protected override void FillTreeWithData(ObjectSelectorEditor.Selector selector, ITypeDescriptorContext context, IServiceProvider provider)
        {
            if (((selector != null) && (context != null)) && ((provider != null) && (context.Instance is Adoor.Object.Domain.IObjectResolver)))
            {
                Type type1 = context.PropertyDescriptor.PropertyType;
                selector.Clear();
                IContainer lContainer = context.Container;

                if (lContainer != null)
                {
                    ComponentCollection lCollection = lContainer.Components;
                    if (lCollection != null)
                    {
                        ObjectSelectorEditor.SelectorNode node1 = selector.AddNode("Existing", this, null);
                        foreach (IComponent component1 in lCollection)
                        {
                            if (!type1.IsInstanceOfType(component1))
                            {
                                continue;
                            }
                            string text1 = string.Empty;
                            ISite site1 = component1.Site;
                            if (site1 != null)
                            {
                                text1 = site1.Name;
                            }
                            selector.AddNode(text1, component1, node1);
                        }
                        selector.AddNode("New", type1, null);
                        selector.AddNode("None", null, null);
                    }
                }
            }
        }

        public override void SetValue(object value)
        {
            if (value != this)
            {
                base.currValue = value;
            }
        }



    }


}
